package com.bwie.service.Impl;

import cn.hutool.http.server.HttpServerRequest;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.Dto.BrandDto;
import com.bwie.utils.JwtsUtils;
import com.bwie.utils.R;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.BrandMapper;
import com.bwie.domain.Brand;
import com.bwie.service.BrandService;

import javax.servlet.http.HttpServletRequest;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService{

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private HttpServletRequest request;

    @Override
    public R getBrandList(BrandDto brandDto) {
        //构建分页参数
        Page<Brand> page = new Page<>(brandDto.getPageNum(), brandDto.getPageSize());

        //构建分页条件
        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Brand::getCreateDate);
        Page<Brand> selectPage = brandMapper.selectPage(page, wrapper
                .like(StringUtils.isNotEmpty(brandDto.getBrandName()),Brand::getBrandName,brandDto.getBrandName())
                .eq(Brand::getDeleteFlag,0));

        //传递前端
        return R.OK(selectPage);
    }

    @Override
    public R saveBrand(Brand brand) {

        //解析 token 获取创建人
        String token = request.getHeader("token");
        Integer idByToken = JwtsUtils.getIdByToken(token);

        //自动添加
        brand.setBrandUse(0);
        brand.setBrandStatus(0);
        brand.setCreateBy(idByToken);
        brand.setCreateDate(new Date());
        brand.setDeleteFlag(0);
        //保存，并返回前端
        int insert = brandMapper.insert(brand);
        return insert > 0 ? R.OK("添加成功"):R.ERROR(-1,"添加失败");
    }

    @Override
    public R upBrandStatusById(Brand brand) {

        String token = request.getHeader("token");
        Integer idByToken = JwtsUtils.getIdByToken(token);


        //查新品牌数据
        Brand byId = this.getById(brand.getBrandId());
        if(ObjectUtils.isEmpty(byId)){
            return R.ERROR(-1,"数据错误");
        }
        //初始化 审计字段
        byId.setUpdateBy(idByToken);
        byId.setUpdateDate(new Date());
        byId.setBrandStatus(brand.getBrandStatus());

         brandMapper.updateById(byId);

        return R.OK();
    }


    @Override
    public R delUpdateById(Integer brandId) {
        String token = request.getHeader("token");
        Integer idByToken = JwtsUtils.getIdByToken(token);


        //查新品牌数据
        Brand byId = this.getById(brandId);
        if(ObjectUtils.isEmpty(byId)){
            return R.ERROR(-1,"数据错误");
        }
        //初始化 审计字段
        byId.setUpdateBy(idByToken);
        byId.setUpdateDate(new Date());
        byId.setDeleteFlag(1);
        byId.setDeleteDate(new Date());

        brandMapper.updateById(byId);

        return R.OK();
    }

    @Override
    public R batchDelByIds(BrandDto brandDto) {

        Brand brand = new Brand();
        brand.setDeleteFlag(1);
        brand.setDeleteDate(new Date());

        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Brand::getBrandId,brandDto.getIds());
        brandMapper.update(brand,wrapper);
        return R.OK();
    }

    @Override
    public R getBrand() {
        List<Brand> brands = brandMapper.selectList(null);
        return R.OK(brands);

    }


}
